package com.sugon.thread;

import java.util.concurrent.atomic.AtomicInteger;

public class TestAtomicInteger {
    static int i = 0 ; //error!
    static AtomicInteger a = new AtomicInteger(0);
    static Integer b = Integer.valueOf(0);//error!
    static MyObject obj = new MyObject();

    public static void main(String[] args) throws Exception{
        Thread[] ts = new Thread[10];
        for(int k = 0 ; k<ts.length ; k++){
            ts[k] = new Thread(new Runnable(){
                public void run(){
                    for(int k = 1 ; k <= 10000; k++){
                        i++;
                        a.incrementAndGet();

                        synchronized(b){
                            b = Integer.valueOf(b.intValue()+1);
                        }

                        synchronized(obj){
                            obj.x++;
                        }
                    }
                }
            });
            ts[k].start();
        }

        for(int k=0; k <ts.length ; k++){
            ts[k].join();
        }

        System.out.println(i);
        System.out.println(a);
        System.out.println(b);
        System.out.println(obj.x);
    }

}
class MyObject{
    public int x=0;
}

